home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROCS.ZIP / RANSEQ.ICN < prev    next >
Text File  |  1992-09-28  |  1KB  |  50 lines

  1. ############################################################################
  2. #
  3. #    File:     ranseq.icn
  4. #
  5. #    Subject:  Procedures to generate all integers over a range, randomly
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     February 9, 1991
  10. #
  11. ###########################################################################
  12. #
  13. #  This procedure generates the integers from i to j in random order.
  14. #
  15. #  A linear congruence relationship is used.  See Knuth, The Art of
  16. #  Computer Programming, Vol.2, Seminumerical Algorithms, pp. 15-24,
  17. #  156-157.
  18. #
  19. #  The constants used here are not selected carefully; the "randomness"
  20. #  of the sequence may not be good.
  21. #
  22. ############################################################################
  23. #
  24. #  Links:  nxtprime
  25. #
  26. ############################################################################
  27.  
  28. link nxtprime
  29.  
  30. procedure ranseq(i, j)
  31.    local x, m, a, c, n
  32.  
  33.    n := j - i + 1
  34.  
  35.    if n < 0 then fail
  36.  
  37.    x := 1
  38.    m := nxtprime(n)
  39.    a := m + 1
  40.    c := nxtprime(m)
  41.  
  42.    every 1 to m do {
  43.       x := (a * x + c) % m
  44.       if x < n then {        # discard out-of-range values
  45.          suspend x + i
  46.          }
  47.       }
  48.  
  49. end
  50.